package com.yiren.manager.ctrl.elder;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.Favorites;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrBehalfWater;
import com.yiren.entity.YrElder;
import com.yiren.entity.YrElderArrearage;
import com.yiren.entity.YrElderBeHospital;
import com.yiren.entity.YrElderPayRecord;
import com.yiren.entity.YrElderWallet;
import com.yiren.manager.form.elder.YrBehalfWaterForm;
import com.yiren.manager.service.iface.elder.YrBehalfWaterService;
import com.yiren.manager.service.iface.elder.YrElderBeHospitalService;
import com.yiren.manager.service.iface.elder.YrElderService;
import com.yiren.manager.service.iface.elder.YrElderWalletService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("behalfWater")
@Controller
public class YrBehalfWaterController extends YrElderBaseController<YrBehalfWaterForm, YrBehalfWater>{

	@Autowired
	private YrBehalfWaterService selfService;
	
	@Autowired
	private YrElderWalletService elderWalletService;
	
	@Autowired
	private YrElderService elderService;
	
	@Autowired
	private YrElderBeHospitalService elderBeHospitalService;
	
	//保存实体
	@RequestMapping("/save")
	@ResponseBody
	public HTTPResult<YrBehalfWaterForm> save(String data) throws SwallowException, MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		HTTPResult<YrBehalfWaterForm> result = new HTTPResult<YrBehalfWaterForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrBehalfWaterForm form = tool.mapper("behalfWater", YrBehalfWaterForm.class);
		if(null==form){
			throwExp("param.error");
		}
		if(null==form.getBehalfWaterId()||form.getBehalfWaterId().isEmpty()){
			form.setBehalfWaterId(UUIDGenerator.getUUID());
			form.setCreateTime(new Date());
		}
		form.setUpdateTime(new Date());
		
		YrElder elder = elderService.findOne(form.getElderId());
		if(null==elder){
			throwExp("查询不到相关老者信息");
		}
		
		String sqlWhere = "elder_id=?1 AND be_status<>2";
		
		QueryResult<YrElderBeHospital> qrElderBeHospital = elderBeHospitalService.find(YrElderBeHospital.class, sqlWhere, new Object[]{form.getElderId()});
		if(qrElderBeHospital.getResultList().isEmpty()){
			throwExp("查询不到相关老者住院登记信息");
		}
		YrElderBeHospital elderBeHospital = qrElderBeHospital.getResultList().get(0);
		
		sqlWhere = "elder_id=?1";

		QueryResult<YrElderWallet> qrElderWallet = elderWalletService.find(YrElderWallet.class, sqlWhere, new Object[]{form.getElderId()});
		if(qrElderWallet.getTotalRecord()<1){
			throwExp("该老者未有钱包");
		}
		
		YrElderWallet elderWallet = qrElderWallet.getResultList().get(0);
		Float money  = elderWallet.getWalletBalance();
		YrElderPayRecord elderPayRecord = null;
		YrElderArrearage elderArrearage = null;
//		if(money-form.getRent()<0){
//			elderArrearage = constElderArreagrage(elderBeHospital.getBeHospitalId(),elder, 5, form.getBehalfWaterId(),
//					form.getRent(), "钱包余额不足", "代缴水费", form.getCreateId());
//			elderWallet = null;
//		}else{
			elderWallet.setWalletBalance(money-form.getRent());
			elderPayRecord = costElderPayRecord(elderBeHospital.getBeHospitalId(),"代缴水费", elder, 5, form.getBehalfWaterId(), form.getRent(),2,0,form.getCreateId(),1);
//		}
		
		YrBehalfWater entity = ObjectUtils.objectToObject(form, YrBehalfWater.class);
		Favorites favorites = new Favorites();
		
		favorites.putFavorite(YrBehalfWater.class, entity);
		favorites.putFavorite(YrElderWallet.class, elderWallet);
		favorites.putFavorite(YrElderPayRecord.class, elderPayRecord);
		favorites.putFavorite(YrElderArrearage.class, elderArrearage);
		
		selfService.addByFavorites(favorites);
		return result;
	}
	
	
	
	
	@Override
	public YrBehalfWaterForm toForm(YrBehalfWater e) throws Exception {
		YrBehalfWaterForm form = ObjectUtils.objectToObject(e, YrBehalfWaterForm.class);
		return form;
	}

	@Override
	public YrBehalfWater toEntity(YrBehalfWaterForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
